package com.team8.auth.service.controller;

import com.team8.auth.common.pojo.UserInfo;
import com.team8.auth.common.utils.JwtUtils;
import com.team8.auth.service.config.JwtProperties;
import com.team8.auth.service.service.AuthService;
import com.team8.common.results.Result;
import com.team8.common.utils.CookieUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@EnableConfigurationProperties(JwtProperties.class)
public class AuthController {
    @Autowired
    private AuthService authService;

    @Autowired
    private JwtProperties prop;

    @PostMapping("accreditCookie")
    public ResponseEntity<Result> accreditCookie(
            @RequestParam("email") String email,
            @RequestParam("pwd") String pwd,
            HttpServletRequest request,
            HttpServletResponse response) {
        // 登录校验
        String token = this.authService.authentication(email, pwd);
        System.out.println(token);
        if (StringUtils.isBlank(token)) {
            return ResponseEntity.ok(Result.getErrorInstance());
        }

        // 将token写入cookie,并指定httpOnly为true，防止通过JS获取和修改
        CookieUtils.setCookie(request, response, prop.getCookieName(),
                token, prop.getCookieMaxAge(), null, true);
        return ResponseEntity.ok(Result.getSuccessInstance());
    }

    @GetMapping("accreditMobile")
    public ResponseEntity<String> accreditMobile(
            @RequestParam("email") String email,
            @RequestParam("pwd") String pwd,
            HttpServletRequest request,
            HttpServletResponse response) {
        // 登录校验
        String token = this.authService.authentication(email, pwd);
        System.out.println(token);
        if (StringUtils.isBlank(token)) {
            return ResponseEntity.ok("error");
        }
        return ResponseEntity.ok(token);
    }
    @GetMapping("verify")
    public ResponseEntity<UserInfo> verifyUser(@CookieValue("AS_TOKEN")String token){
        try {
            // 从token中解析token信息
            UserInfo userInfo = JwtUtils.getInfoFromToken(token, this.prop.getPublicKey());
            // 解析成功返回用户信息
            return ResponseEntity.ok(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 出现异常则，响应500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("verifyMobile")
    public ResponseEntity<UserInfo> verifyMobile(@RequestParam("token")String token){
        try {
            // 从token中解析token信息
            UserInfo userInfo = JwtUtils.getInfoFromToken(token, this.prop.getPublicKey());
            // 解析成功返回用户信息
            return ResponseEntity.ok(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 出现异常则，响应500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
